const express = require('express');
const path = require('node:path');
const pool = require(path.resolve(__dirname, '../sqlPool.js'));
const router = express.Router();

//指定博客下的未审核评论列表分页查询
router.post('/getAppointBlogComment', (req, res) => {
	if (req.body.blogId && req.body.limit) {
		let sql = `SELECT * FROM commentmsg WHERE blogId=${req.body.blogId} AND state="pending" LIMIT ${req.body.limit} OFFSET ${req.body.offset}`;
		pool.query(sql, (error, result) => {
			if (error) {
				console.log(error);
				return res.send({
					state: 'error',
				});
			} else {
				return res.send({
					state: 'ok',
					data: result,
				});
			}
		});
	} else {
		return res.send({
			state: 'error',
		});
	}
});

//获取指定博客下的未审核评论列表长度
router.post('/getAppointBlogCommentTotal', (req, res) => {
	if (req.body.blogId) {
		let sql = `SELECT * FROM commentmsg WHERE blogId=${req.body.blogId} AND state="pending"`;
		pool.query(sql, (error, result) => {
			if (error) {
				console.log(error);
				return res.send({
					state: 'error',
				});
			} else {
				return res.send({
					state: 'ok',
					data: result.length,
				});
			}
		});
	} else {
		return res.send({
			state: 'error',
		});
	}
});

//留言板评论的分页查询
router.post('/getBoardPageComment', (req, res) => {
	if (req.body.limit) {
		let sql = `SELECT * FROM commentmsg WHERE reviewee="#board#" AND state="pending" LIMIT ${req.body.limit} OFFSET ${req.body.offset}`;
		pool.query(sql, (error, result) => {
			if (error) {
				console.log(error);
				return res.send({
					state: 'error',
				});
			} else {
				return res.send({
					state: 'ok',
					data: result,
				});
			}
		});
	}
});

//获取留言板评论列表长度
router.post('/getBoardCommentTotal', (req, res) => {
	let sql = `SELECT * FROM commentmsg WHERE reviewee="#board#" AND state="pending"`;
	pool.query(sql, (error, result) => {
		if (error) {
			console.log(error);
			return res.send({
				state: 'error',
			});
		} else {
			return res.send({
				state: 'ok',
				data: result.length,
			});
		}
	});
});

//更新评论状态
router.post('/updateCommentState', (req, res) => {
	if (req.body.state && req.body.id) {
		let sql = `UPDATE commentmsg SET state="${req.body.state}" WHERE id=${req.body.id}`;
		pool.query(sql, (error, result) => {
			if (error || result.changedRows === 0) {
				console.log(error);
				return res.send({
					state: 'error',
				});
			} else {
				return res.send({
					state: 'ok',
				});
			}
		});
	}
});

router.post('/addBlogComment', (req, res) => {
	if (req.body.content && req.body.commentator && req.body.reviewee) {
		let sql = `INSERT INTO commentmsg(content,commentator,reviewee,blogId,state) VALUES('${req.body.content}','${req.body.commentator}','${req.body.reviewee}',${req.body.blogId},"pending")`;
		pool.query(sql, (error, result) => {
			if (error) {
				console.log(error);
				return res.send({
					state: 'error',
				});
			} else {
				return res.send({
					state: 'ok',
				});
			}
		});
	} else {
		return res.send({
			state: 'error',
		});
	}
});

router.post('/getBlogComment', (req, res) => {
	if (req.body.blogId || req.body.blogId === 0) {
		let sql = `SELECT * FROM commentmsg WHERE blogId=${req.body.blogId} AND state="fulfilled"`;
		pool.query(sql, (error, result) => {
			if (error) {
				console.log(error);
				return res.send({
					state: 'error',
				});
			} else if (result.length === 0) {
				return res.send({
					state: 'none',
				});
			} else {
				return res.send({
					state: 'ok',
					data: result,
				});
			}
		});
	} else {
		return res.send({
			state: 'error',
		});
	}
});

module.exports = router;
